package cn.com.blueInfo.bpm.number.service.impl;

import cn.com.blueInfo.bpm.number.convert.NumberSerialConvert;
import cn.com.blueInfo.bpm.number.entity.NumberSerial;
import cn.com.blueInfo.bpm.number.repository.NumberSerialRepository;
import cn.com.blueInfo.bpm.number.request.NumberSerialDTO;
import cn.com.blueInfo.bpm.number.response.NumberSerialVO;
import cn.com.blueInfo.bpm.number.service.NumberSerialService;
import cn.com.blueInfo.core.util.SnowflakeIdGenerator;
import cn.com.blueInfo.utils.result.ResultInfo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 编号规则流水号表 业务服务实现类
 * @author suxch
 * @since 2025-09-01 19:26:29
 */
@Log4j2
@Service
public class NumberSerialServiceImpl implements NumberSerialService {

    @Autowired
    private NumberSerialRepository numberSerialRepository;

    @Autowired
    private SnowflakeIdGenerator snowflakeIdGenerator;

    @Autowired
    private NumberSerialConvert numberSerialConvert;

    /**
     * 新增编号规则流水号
     * @param numberSerialDTO 编号规则流水号参数
     */
    @Override
    public ResultInfo<Integer> addNumberSerial(NumberSerialDTO numberSerialDTO) {
        NumberSerial numberSerial = numberSerialConvert.dtoToEntity(numberSerialDTO);
        numberSerial.setId(snowflakeIdGenerator.nextId());
        Integer addSize = numberSerialRepository.createNumberSerial(numberSerial);
        return ResultInfo.<Integer>builder().data(addSize).message("编号规则流水号成功新增 {} 条").build();
    }

    /**
     * 批量新增编号规则流水号
     * @param numberSerialDTOList 编号规则流水号参数列表
     */
    @Override
    public ResultInfo<Integer> batchAddNumberSerial(List<NumberSerialDTO> numberSerialDTOList) {
        List<NumberSerial> numberSerialList = numberSerialConvert.dtoToEntity(numberSerialDTOList);
        for (NumberSerial numberSerial : numberSerialList) numberSerial.setId(snowflakeIdGenerator.nextId());
        Integer addSize = numberSerialRepository.batchCreateNumberSerial(numberSerialList);
        return ResultInfo.<Integer>builder().data(addSize).message("编号规则流水号成功新增 {} 条").build();
    }

    /**
     * 删除编号规则流水号
     * @param numberSerialId 编号规则流水号ID
     */
    @Override
    public ResultInfo<Integer> removeNumberSerial(Long numberSerialId) {
        Integer removeSize = numberSerialRepository.deleteNumberSerial(numberSerialId);
        return ResultInfo.<Integer>builder().data(removeSize).message("编号规则流水号成功删除 {} 条").build();
    }

    /**
     * 批量删除编号规则流水号
     * @param numberSerialIdList 编号规则流水号ID列表
     */
    @Override
    public ResultInfo<Integer> batchRemoveNumberSerial(List<Long> numberSerialIdList) {
        Integer removeSize = numberSerialRepository.batchDeleteNumberSerial(numberSerialIdList);
        return ResultInfo.<Integer>builder().data(removeSize).message("编号规则流水号成功删除 {} 条").build();
    }

    /**
     * 更新部分编号规则流水号
     * @param numberSerialUpdatePartialDTO 编号规则流水号参数
     */
    @Override
    public ResultInfo<Integer> modifyNumberSerialPartial(NumberSerialDTO.UpdatePartialDTO numberSerialUpdatePartialDTO) {
        NumberSerial numberSerial = numberSerialConvert.dtoToEntity(numberSerialUpdatePartialDTO);
        Integer modifySize = numberSerialRepository.updateNumberSerialPartial(numberSerial);
        return ResultInfo.<Integer>builder().data(modifySize).message("编号规则流水号成功更新 {} 条").build();
    }

    /**
     * 更新全部编号规则流水号
     * @param numberSerialDTO 编号规则流水号参数
     */
    @Override
    public ResultInfo<Integer> modifyNumberSerial(NumberSerialDTO numberSerialDTO) {
        NumberSerial numberSerial = numberSerialConvert.dtoToEntity(numberSerialDTO);
        Integer modifySize = numberSerialRepository.updateNumberSerial(numberSerial);
        return ResultInfo.<Integer>builder().data(modifySize).message("编号规则流水号成功更新 {} 条").build();
    }

    /**
     * 查询一条编号规则流水号
     * @param numberSerialId 编号规则流水号ID
     */
    @Override
    public ResultInfo<NumberSerialVO> queryNumberSerialById(Long numberSerialId) {
        NumberSerial numberSerial = numberSerialRepository.getNumberSerialById(numberSerialId);
        NumberSerialVO numberSerialVO = numberSerialConvert.entityToVO(numberSerial);
        return ResultInfo.<NumberSerialVO>builder().data(numberSerialVO).message("编号规则流水号查询成功").build();
    }

    /**
     * 分页查询编号规则流水号
     * @param numberSerialQueryDTO 编号规则流水号参数
     */
    @Override
    public ResultInfo<Page<NumberSerialVO>> queryNumberSerialPage(NumberSerialDTO.QueryDTO numberSerialQueryDTO) {
        Page<NumberSerial> numberSerialPage = numberSerialRepository.getNumberSerialPage(numberSerialQueryDTO);
        Page<NumberSerialVO> resultData = numberSerialConvert.convertPage(numberSerialPage);
        return ResultInfo.<Page<NumberSerialVO>>builder()
                .data(resultData)
                .message("编号规则流水号分页查询成功，共 {} 条", resultData.getRecords().size())
                .build();
    }

    /**
     * 根据条件查询 编号规则流水号 数量
     * @param numberSerialQueryDTO 编号规则流水号参数
     */
    @Override
    public ResultInfo<Long> queryNumberSerialCount(NumberSerialDTO.QueryDTO numberSerialQueryDTO) {
        Long resultCount = numberSerialRepository.getNumberSerialCount(numberSerialQueryDTO);
        return ResultInfo.<Long>builder()
                .data(resultCount)
                .message("编号规则流水号数量查询成功，共 {} 条", resultCount)
                .build();
    }

    /**
     * 根据条件查询 编号规则流水号
     * @param numberSerialQueryDTO 编号规则流水号参数
     */
    @Override
    public ResultInfo<List<NumberSerialVO>> queryNumberSerialList(NumberSerialDTO.QueryDTO numberSerialQueryDTO) {
        List<NumberSerial> numberSerialList = numberSerialRepository.getNumberSerialList(numberSerialQueryDTO);
        List<NumberSerialVO> resultData = numberSerialConvert.entityListToVoList(numberSerialList);
        return ResultInfo.<List<NumberSerialVO>>builder()
                .data(resultData)
                .message("编号规则流水号分页查询成功，共 {} 条", resultData.size())
                .build();
    }
}
